package com.jun.leetcode.chapter1;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.text.DecimalFormat;

/**
 * 描述:核心思想是两个数组切一刀，用极限来做比较
 *
 * @author Administrator
 * @create 2018-09-20 21:24
 */
public class Solution4 {
    public double findMedianSortedArrays(int[] nums1, int[] nums2) {
        int len1 = nums1.length;
        int len2 = nums2.length;
        int result = 0 ;
        boolean flag = false;

        if ( len1 > len2 ){
            int[] numtemp = new int[len1];
            numtemp = nums1;
            nums1 = nums2;
            nums2 = numtemp;

            int temp = len1;
            len1 = len2 ;
            len2 = temp;
        }

        int iMin = 0,iMax = len1 , halfLen = ( len1 + len2 +1 )/2;
        while ( iMin <= iMax){
            int i = (iMin + iMax )/2;
            int j = halfLen - i ;
            if ( i < iMax && nums2[j-1] > nums1[i]){
                iMin = i + 1;
            }else if ( i > iMin && nums1[i-1] > nums2[j]){
                iMax = i - 1;
            }else {
                int maxLeft = 0 ;
                if ( i== 0 ){
                    maxLeft = nums2[j-1];
                }else if ( j == 0 ){
                    maxLeft = nums1[i-1];
                }else {
                    maxLeft = Math.max( nums1[i-1] , nums2[j-1]);
                }
                if ( ( len1 + len2 ) %2 == 1 ) {
                    return maxLeft;
                }

                int minRigth = 0 ;
                if ( i == len1 ){
                    minRigth = nums2[j];
                }else if ( j == len2 ){
                    minRigth = nums1[i];
                }else {
                    minRigth = Math.min(nums2[j] , nums1[i] );
                }

                return ( maxLeft + minRigth ) / 2.0;
            }
        }
        return 0.0;
    }

}

class Class4 {
    public static int[] stringToIntegerArray(String input) {
        input = input.trim();
        input = input.substring(1, input.length() - 1);
        if (input.length() == 0) {
            return new int[0];
        }

        String[] parts = input.split(",");
        int[] output = new int[parts.length];
        for(int index = 0; index < parts.length; index++) {
            String part = parts[index].trim();
            output[index] = Integer.parseInt(part);
        }
        return output;
    }

    public static String doubleToString(Double input) {
        return new DecimalFormat("0.00000").format(input);
    }

    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        String line;
        while ((line = in.readLine()) != null) {
            int[] nums1 = stringToIntegerArray(line);
            line = in.readLine();
            int[] nums2 = stringToIntegerArray(line);

            double ret = new Solution4().findMedianSortedArrays(nums1, nums2);

            String out = doubleToString(ret);

            System.out.print(out);
        }
    }
}